<?php 
/**
 * 订单控制器
 */
class OrdersController extends CommonController{
	private $ordersModel;
	public function __init(){
		if(!isset($_SESSION['uid'])) $this->error('请登录用户');
		$this->ordersModel = K('Orders');
	}
	
	/**
	 * 加载订单确认页
	 * 对订单的处理：通过提交的sid，从购物车提取出用户选择的商品信息，分配到订单页中
	 * 地址处理：调取用户表中的地址信息作为默认收货地址
	 */
	public function index(){
		//处理购物车提交上来的数据为post提交的sid,立即购买是get提交上来的sid
		//获取sid
		$sid = isset($_POST['sid']) ? Q('post.') : $_GET['sid'];
		//将get上来的sid转换成与post上来的数据一致的数据形式
		if(!is_array($sid)){
			$sid = array('sid' => array($sid));
		}
		if($sid){
			//重组确认订单数组
			$confirmData = array();
			foreach ($sid['sid'] as $v) {
				$confirmData[] = $_SESSION['cart']['goods'][$v];
			}
			$this->assign('confirmData',$confirmData);
			//计算总价格、运费计算，多件商品取商品的最高运费
			$totalPrice = 0;
			$postage = array();
			foreach ($confirmData as $value) {
				$totalPrice += $value['total'];
				$postage[] = current(K('Goods')->field('postage')->where("gid={$value['id']}")->find());
			}
			//排序取数组最大值
			sort($postage);
			$postage = array_pop($postage);
			//计算合计的价格：运费加商品总价格
			$totalPrice = $totalPrice + $postage;
			$this->assign('postage',$postage);
			$this->assign('totalPrice',$totalPrice);
//			p($confirmData);
			//将确认订单的sid放到session中
			$_SESSION['cart']['confirmSid'] = $sid;
			//将价格放到session中
			$_SESSION['cart']['totalPrice'] = $totalPrice;
			//调取默认地址
			$addData = K('user')->where("uid={$_SESSION['uid']}")->find();
			$this->assign('addData',$addData);
		}

		$this->navigation();
		$this->display();
	}
	
	
	/**
	 * 提交订单
	 */
	public function confirmOrder(){

		if(!$_SESSION['cart']['confirmSid'] && $_SESSION['uid']) $this->error('订单提交失败，请重新登录');
		$sid = $_SESSION['cart']['confirmSid'];
		//将数据写到数据库中
		if(IS_POST){
			if(!$this->ordersModel->addOrders()) $this->error($this->ordersModel->error,U('Index/Carts/index'));
			$this->success('订单提交成功',U('Index/Orders/myorder'));
		}
		
		
	}
	
	/**
	 * 我的订单、订单状态
	 */
	public function myOrder(){
		$os = Q('get.os',1,'intval');
		//显示所有订单，待付款，已收货
		if($os==0 || $os==2){
			$orderData = M('')->join('__orders__ as o JOIN __orders_list__ as ol ON o.oid=ol.orders_oid')->where("state={$os} AND user_uid={$_SESSION['uid']}")->all();
		}else{
			$orderData = M('')->join('__orders__ as o JOIN __orders_list__ as ol ON o.oid=ol.orders_oid')->where("user_uid={$_SESSION['uid']}")->all();
		}
		$this->assign('orderData',$orderData);
		
		//统计订单数
		$orderCount = $this->ordersModel->where("user_uid={$_SESSION['uid']}")->count();
		$this->assign('orderCount',$orderCount);
		
		//统计购物车
		if(isset($_SESSION['cart'])){
			$cartCount = count($_SESSION['cart']['goods']);
			$this->assign('cartCount',$cartCount);
		}

		//统计喜欢数
		$myLikes = K("GoodsUser")->where("user_uid={$_SESSION['uid']}")->count();
		$this->assign('myLikes',$myLikes); 
		$this->navigation();
		$this->display();	
			
		
	}
	
	/**
	 * 异步处理取消订单
	 */
	
	public function cancelOrder(){
		if(!IS_AJAX) $this->error('非法请求');
		//删除olid
		$olid = Q('post.olid');
		$data = K('OrdersList')->where("olid={$olid}")->find();
		$oid = $data['orders_oid'];
		K('OrdersList')->where("olid={$olid}")->delete();
		//订单表中是否有订单，没有则删除此订单
		$listData = K('OrdersList')->where("orders_oid={$oid}")->all();
		if(!$listData){
			K('Orders')->where("oid={$oid}")->delete();
		}
		echo 1;die;
		
	}

	/**
	 * 确认发货处理
	 */
	public function confirm(){
		//修改订单状态
		$oid = Q('get.oid',0,'intval');
		if($oid){
			$data = array(
				'oid' => $oid,
				'state' => 3
			);
			K('Orders')->update($data);
			$this->success('确认收货成功');
		}
		
		
	}
	
	/**
	 * 异步处理收货地址
	 * 将地址写人用户表中，此用户在处理订单的时候调用此用户表中的地址作为其默认收货地址
	 */
	 public function updateAdd(){
	 	if(!IS_AJAX) $this->error('非法请求');
		//修改user表将收货地址插入
		$data = array(
			'uid' 		=> $_SESSION['uid'],
			'realname'	=> Q('post.consignee'),
			'add'		=> Q('post.address'),
			'phone'		=> Q('post.mobile')
		);
		K('User')->update($data);
		
	 }
	
	
	
	
	
	
	
	
	
	
	
}






















 ?>